package org.chatboleh.util;

import java.text.DecimalFormat;

/**
 * 
 * @author caslyn
 * 
 */
public class CurrencyToWords {
	private final static String[] BI_ONES = { "", "one ", "two ", "three ",
			"four ", "five ", "six ", "seven ", "eight ", "nine " };
	private final static String[] BI_TEENS = { "ten ", "eleven ", "twelve ",
			"thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ",
			"eighteen ", "nineteen " };
	private final static String[] BI_TENS = { "", "", "twenty ", "thirty ",
			"forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety " };
	private final static String[] BI_SUFFIX = { "hundred ", "thousand ",
			"million ", "billion ", "trillion ", "quadrillion " };

	private final static String[] BM_ONES = { "", "satu ", "dua ", "tiga ",
			"empat ", "lima ", "enam ", "tujuh ", "lapan ", "sembilan " };
	private final static String[] BM_TEENS = { "sepuluh ", "sebelas ",
			"dua belas ", "tiga belas ", "empat belas ", "lima belas ",
			"enam belas ", "tujuh belas ", "lapan belas ", "sembilan belas " };
	private final static String[] BM_TENS = { "", "", "dua puluh ",
			"tiga puluh ", "empat puluh ", "lima puluh ", "enam puluh ",
			"tujuh puluh ", "lapan puluh ", "sembilan puluh " };
	private final static String[] BM_SUFFIX = { "ratus ", "ribu ", "juta ",
			"billion ", "trillion ", "quadrillion " };
	private final static String BI_AND_CENTS = "and cents ";
	private final static String BM_AND_CENTS = "dan sen ";

	public static String convert(long money, int l) {
		return convert(new DecimalFormat("#0.00").format(money), l);
	}

	public static String convert(double money, int l) {
		return convert(new DecimalFormat("#0.00").format(money), l);
	}

	public static String convert(float money, int l) {
		return convert(new DecimalFormat("#0.00").format(money), l);
	}

	/**
	 * @param String
	 *            money
	 * @param int l (Language.ENGLISH, Language.MALAY)
	 * @return String
	 */
	public static String convert(String money, int lang) {
		money = money.trim().replaceAll(" ", "").replaceAll(",", "");
		return doConvert(money, lang);
	}

	private static String getONES(int index, int lang) {
		if (lang == Language.MALAY) {
			return BM_ONES[index];
		} else {
			return BI_ONES[index]; // default ENGLISH
		}

	}

	private static String getTENS(int index, int lang) {
		if (lang == Language.MALAY) {
			return BM_TENS[index];
		} else {
			return BI_TENS[index]; // default ENGLISH
		}

	}

	private static String getTEENS(int index, int lang) {
		if (lang == Language.MALAY) {
			return BM_TEENS[index];
		} else {
			return BI_TEENS[index]; // default ENGLISH
		}

	}

	private static String getSUFFIX(int index, int lang) {
		if (lang == Language.MALAY) {
			return BM_SUFFIX[index];
		} else {
			return BI_SUFFIX[index]; // default ENGLISH
		}

	}

	private static String getAndCents(int lang) {
		if (lang == Language.MALAY) {
			return BM_AND_CENTS;
		} else {
			return BI_AND_CENTS; // default ENGLISH
		}

	}

	private static String doConvert(String money, int lang) {
		String dollars = "";
		String cents = "";
		String result = "";

		if (money.indexOf(".") > 0) {
			dollars = money.split("\\.")[0];
			cents = money.split("\\.")[1];
		} else {
			dollars = money;
		}

		int count = 0;
		String tdigit = "";
		// Read from end
		for (int i = dollars.length(); i > 0; i--) {
			tdigit = dollars.charAt(i - 1) + tdigit;
			if (tdigit.length() == 3) {
				// process the digits
				int x = Integer.parseInt(tdigit.substring(1, 3));
				int y = Integer.parseInt(tdigit.substring(0, 1));
				if (count > 0) {
					result = getSUFFIX(count, lang) + result;
				}
				if (x < 20) {
					if (x < 10) {
						result = "and "
								+ getONES(Integer.parseInt(""
										+ tdigit.charAt(2)), lang) + result;
					} else {
						result = getTEENS(Integer.parseInt(""
								+ tdigit.charAt(2)), lang)
								+ result;
					}
				} else {
					result = getONES(Integer.parseInt("" + tdigit.charAt(2)),
							lang)
							+ result;
					result = getTENS(Integer.parseInt("" + tdigit.charAt(1)),
							lang)
							+ result;
				}
				if (y > 0) {
					result = getSUFFIX(0, lang) + result;
					result = getONES(Integer.parseInt("" + tdigit.charAt(0)),
							lang)
							+ result;
				}
				tdigit = "";
				count++;
			}

		}

		if (tdigit.length() > 0) { // < 3
			// process the digits
			int x = Integer.parseInt(tdigit);
			// int y = Integer.parseInt(tdigit.substring(0, 1));
			if (count > 0) {
				result = getSUFFIX(count, lang) + result;
			}
			if (x < 20) {
				if (x < 10) {
					result = getONES(Integer.parseInt("" + tdigit.charAt(0)),
							lang)
							+ result;
				} else {
					result = getTEENS(Integer.parseInt("" + tdigit.charAt(1)),
							lang)
							+ result;
				}
			} else {
				result = getONES(Integer.parseInt("" + tdigit.charAt(1)), lang)
						+ result;
				result = getTENS(Integer.parseInt("" + tdigit.charAt(0)), lang)
						+ result;
			}
			tdigit = "";
			count++;
		}

		// Process CENTS
		if (cents.length() > 0) { // < 3
			// process the digits
			int x = Integer.parseInt(cents);

			if (x > 0) {
				result += getAndCents(lang);
				if (x < 20) {
					if (x < 10) {
						result += getONES(Integer
								.parseInt("" + cents.charAt(0)), lang);
					} else {
						result += getTEENS(Integer.parseInt(""
								+ cents.charAt(1)), lang);
					}
				} else {
					result += getTENS(Integer.parseInt("" + cents.charAt(0)),
							lang);
					result += getONES(Integer.parseInt("" + cents.charAt(1)),
							lang);
				}
			}
		}

		return result;
	}

	public static String toProperCase(String name) {
		if(name == null){
			return "";
		} else if (name.length() > 0) {
			name = name.toLowerCase();
			name = name.substring(0, 1).toUpperCase();
		}
		return name;
	}

}
